#include "task.h"

/**
 * @brief 任务结构体初始化
 * 
 * @param srctask 任务结构体的指针
 * @param task_f 任务函数指针
 * @param count 运行次数，-1为无限循环运行
 */
void TaskInit(Task_t *srctask, void (*task_f)(void), uint32_t period, int32_t count)
{
    srctask->TargetTick = HAL_GetTick() + period;
    srctask->period = period;
    srctask->count = count;
    srctask->task = task_f;
}

/**
 * @brief 定时运行任务
 * 
 * @param srctask 任务结构体的指针
 */
void RunTask(Task_t *srctask)
{
    uint32_t CurTick = HAL_GetTick();
    if (srctask->count != 0)
    {
        if (CurTick - srctask->TargetTick > UINT32_MAX / 2)
            return;
        srctask->task();
        srctask->TargetTick = CurTick + srctask->period;
        if (srctask->count > 0)
            srctask->count--;
    }
    else
        return;
}